//
//  ViewController.m
//  60003-验证码
//
//  Created by student on 16/3/6.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *label;

@property (strong,nonatomic) NSArray *dataArr;

@property (strong,nonatomic) NSArray *nameArr;

@property (strong,nonatomic) NSArray *picArr;

@property (strong,nonatomic) NSArray *btnArr;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSString *path = [[NSBundle mainBundle] pathForResource:@"codes.plist" ofType:nil];
    self.dataArr = [NSArray arrayWithContentsOfFile:path];
    NSMutableArray *tempName = [NSMutableArray array];
    NSMutableArray *tempPic = [NSMutableArray array];
    for (NSDictionary *dic in self.dataArr) {
        NSString *str = dic[@"chinese"];
        [tempName addObject:str];
        NSArray *fruitArr = dic[@"images"];
        for (NSString *str1 in fruitArr) {
            [tempPic addObject:str1];
        }
    }
    self.nameArr = tempName;
    self.picArr = tempPic;
    [self setUpBtn];
    BOOL m = NO; //用来标记第一次显示的页面中  图片必须只有有一张与label中的内容匹配;
    do {
        int n = arc4random()%self.nameArr.count;
        self.label.text = self.nameArr[n];
        for (UIButton * btn in self.btnArr) {
            if (btn.tag/100 == n) {
                m = YES;
            }
        }
    }while (m == NO);
}
//界面上添加8个按钮
-(void)setUpBtn{
    int x = [UIScreen mainScreen].bounds.size.width;
    NSMutableArray *tempBtn = [NSMutableArray array];
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 4; j++) {
            UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(x/4*j, 110+i*94, 93, 94)];
            int m = arc4random()%self.picArr.count;
            int s1 = m%6;
            int s2 = m/6;
            [btn setImage:[UIImage imageNamed:self.picArr[m]] forState:UIControlStateNormal];
            [self.view addSubview:btn];
            [btn addTarget:self action:@selector(sureBtn:) forControlEvents:UIControlEventTouchUpInside];
            btn.tag = s2*100 + s1;
            [tempBtn addObject:btn];
            
        }
    }
    self.btnArr = tempBtn;
}

//点击图片Btn的实现方法（如有有勾去去掉，如果没有就勾上）
-(void)sureBtn:(UIButton *)sender{
    
    if (sender.subviews.count == 1) {
        int x = sender.frame.size.width;
        int y = sender.frame.size.height;
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(x-10, y-10, 10, 10)];
        view.backgroundColor = [UIColor greenColor];
        [sender addSubview:view];
    }
    else{
        for (UIView *v in sender.subviews) {
            if ([v isKindOfClass:[UIView class]] && ![v isKindOfClass:[UIImageView class]]) {
                [v removeFromSuperview];
            }
        }
    }
}
//刷新按钮的实现方法
- (IBAction)pressBtn:(UIButton *)sender {
    //必须要有一张图片的内容和label中的内容对应的做法
    int s = 0;
    do {
        int s8 = arc4random()%self.nameArr.count;
        self.label.text = self.nameArr[s8];
        for (UIButton *btn in self.btnArr) {
            int m = arc4random()%self.picArr.count;
            int s1 = m%6;
            int s2 = m/6;
            [btn setImage:[UIImage imageNamed:self.picArr[m]] forState:UIControlStateNormal];
            btn.tag = s2*100 + s1;
            if (s2 == s8) {
                s = 1;
            }
            
        }
        for (UIButton *btn in self.btnArr) {
            for (UIView *v in btn.subviews) {
                if ([v isKindOfClass:[UIView class]] && ![v isKindOfClass:[UIImageView class]]) {
                    [v removeFromSuperview];
                }
            }
        }
            }while (s == 0);
}
//登陆按钮的实现方法
- (IBAction)perssLoadBtn:(UIButton *)sender {
    int s0 = 0;
    int s1 = 0;
    int m0 = 0;
    int m1 = 0;
    for (int i = 0; i < 5; i++) {
        if ([self.label.text isEqualToString:self.nameArr[i]]) {
            for (UIButton *btn in self.btnArr) {
                if (btn.tag / 100 == i)
                {
                    s0 ++;
                    if (btn.subviews.count > 1) {
                        s1 ++;
                    }
                }
                if (btn.tag / 100 != i) {
                    m0 ++;
                    if (btn.subviews.count == 1) {
                        m1 ++;
                    }
                }
            }
        }
    }
    if (s0 == s1 && m0 == m1) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"验证正确" message:@"进入下个页面" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
        [alert addAction:action];
        [self presentViewController:alert animated:YES completion:nil];
    }
    else{
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"验证错误" message:@"重新验证" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
        [alert addAction:action];
        [self presentViewController:alert animated:YES completion:nil];
    }
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
